package com.qihang.util;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;


public class Tools {

	private static final Logger log = Logger.getLogger(Tools.class);
	
	public static String filterDangerString(String message) {
		if (message == null) {
			return null;
		}
		char content[] = new char[message.length()];
		message.getChars(0, message.length(), content, 0);
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < content.length; i++) {
			switch (content[i]) {
			case '<':
				result.append("&lt;");
				break;
			case '>':
				result.append("&gt;");
				break;
			case '&':
				result.append("&amp;");
				break;
			case '"':
				result.append("&quot;");
				break;
			default:
				result.append(content[i]);
			}
		}
		return result.toString();
	}
	public static void htmlEscape(Object obj) throws Exception {

		if (obj instanceof Short) {
			log.debug(obj + " instanceof Short暂无法处理");
		} else if (obj instanceof Integer) {
			log.debug(obj + " instanceof Integer暂无法处理");
		} else if (obj instanceof Long) {
			log.debug(obj + " instanceof Long暂无法处理");
		} else if (obj instanceof Float) {
			log.debug(obj + " instanceof Float暂无法处理");
		} else if (obj instanceof Double) {
			log.debug(obj + " instanceof Double暂无法处理");
		} else if (obj instanceof Character) {
			log.debug(obj + " instanceof Character暂无法处理");
		} else if (obj instanceof Boolean) {
			log.debug(obj + " instanceof Boolean暂无法处理");
		} else if (obj instanceof Byte) {
			log.debug(obj + " instanceof Byte暂无法处理");
		} else if (obj instanceof String) {
			log.debug(obj + " instanceof String暂无法处理");
		} else if (obj instanceof Map) {
			log.debug(obj + " instanceof Map暂不需做处理");
		} else if (obj instanceof List) {
			log.debug(obj + " instanceof List暂不需做处理");
		} else if (obj instanceof ServletRequest) {
			log.debug(obj + " instanceof ServletRequest暂不需做处理");
			//obj = new TpMultipartHttpServletRequest((HttpServletRequest)obj);
		} else if (obj instanceof MultipartFile) {
			log.debug(obj + " instanceof MultipartFile暂不需做处理");
		} else {
			Field[] fs = obj.getClass().getDeclaredFields();
			for (int i = 0; i < fs.length; i++) {
				fs[i].setAccessible(true);
				if (fs[i].get(obj) instanceof String) {
					fs[i].set(obj, filterDangerString((String) fs[i].get(obj)));
				}
			}
		}
	}
	
	
}
